home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / Sound.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  5.2 KB  |  192 lines  |  [TEXT/CWIE]

  1. // Sound.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8. import java.net.URL;
  9.  
  10.  
  11. /** Object subclass representing a sound.  Typically, you retrieve a Sound
  12.   * by name from an 8 bit, µlaw, 8000 Hz, one-channel, Sun ".au" file:
  13.   * <pre>
  14.   *     sound = Sound.soundNamed("MySound.au");
  15.   * </pre>
  16.   */
  17.  
  18.  
  19. public class Sound implements Codable {
  20.     String                name;
  21.     java.applet.AudioClip awtSound;
  22.     boolean               shouldLoop;
  23.  
  24.     static final String         NAME_KEY = "name";
  25.  
  26.     /** Constructs a Sound with no sound data. This method is only useful
  27.       * when decoding.
  28.       */
  29.     public Sound() {
  30.         super();
  31.     }
  32.  
  33.     /** Returns the Sound named <b>soundName</b>. The application maintains
  34.       * a cache of named Sounds that it checks first. If not located in the
  35.       * cache, this method looks for the specified Sound in the "sounds"
  36.       * direcotry in the same directory as the Application's index.html
  37.       * file.  In other words, it constructs the URL
  38.       * <pre>
  39.       *     "codebase"/sounds/soundName
  40.       * </pre>
  41.       * and attempts to load this file.  <b>soundName</b> can specify a
  42.       * file name or path, such as "newSounds/MySound.au".
  43.       *
  44.       * <p><i><b>Note:</b> No error will be reported if the specified Sound
  45.       * file does not exist or is the wrong format.</i>
  46.       */
  47.     public static synchronized Sound soundNamed(String soundName) {
  48.         Application app;
  49.         Sound sound;
  50.         URL url;
  51.  
  52.         if (soundName == null || soundName.equals(""))
  53.             return null;
  54.  
  55.         app = Application.application();
  56.         sound = (Sound)app.soundByName.get(soundName);
  57.  
  58.         if (sound != null)
  59.             return sound;
  60.  
  61.         url = app._appResources.urlForSoundNamed(soundName);
  62.         sound = soundFromURL(url);
  63.  
  64.         // ALERT.  This is a bogus check.  The AWT never returns null.
  65.         if (sound == null) {
  66.             System.err.println("Unknown sound: " + url);
  67.             return null;
  68.         }
  69.  
  70.         app.soundByName.put(soundName, sound);
  71.         sound.name = soundName;
  72.  
  73.         return sound;
  74.     }
  75.  
  76.     /** Returns a Sound initialized with data from <b>url</b>.
  77.       */
  78.     public static Sound soundFromURL(URL url) {
  79.         java.applet.AudioClip awtSound;
  80.         Sound sound;
  81.  
  82.         awtSound = AWTCompatibility.awtApplet().getAudioClip(url);
  83.  
  84.         sound = new Sound();
  85.         sound.awtSound = awtSound;
  86.  
  87.         return sound;
  88.     }
  89.  
  90.     // This is used in decoding.  Take a look at this again to see if it
  91.     // causes problems by having multiple sounds with the same name.  ALERT!
  92.  
  93.     synchronized void nameSound(String soundName, Sound sound) {
  94.         Application.application().soundByName.put(soundName, sound);
  95.     }
  96.  
  97.     /** Returns the Sound's name, if any.
  98.       */
  99.     public String name() {
  100.         return name;
  101.     }
  102.  
  103.     /** Sets the Sound to automatically replay upon completion, or just play
  104.       * once to completion.
  105.       */
  106.     public void setLoops(boolean flag) {
  107.         shouldLoop = flag;
  108.     }
  109.  
  110.     /** Returns <b>true</b> if the Sound automatically replays upon
  111.       * completion.
  112.       * @see #setLoops
  113.       */
  114.     public boolean doesLoop() {
  115.         return shouldLoop;
  116.     }
  117.  
  118.     /** Plays the Sound.
  119.       */
  120.     public void play() {
  121.         if (awtSound != null) {
  122.             if (shouldLoop) {
  123.                 awtSound.loop();
  124.             } else {
  125.                 awtSound.play();
  126.             }
  127.         }
  128.     }
  129.  
  130.     /** Stops the Sound playback.
  131.       */
  132.     public void stop() {
  133.         if (awtSound != null) {
  134.             awtSound.stop();
  135.         }
  136.     }
  137.  
  138.     /** Returns the Sound's String representation.
  139.       */
  140.     public String toString() {
  141.         if (name != null) {
  142.             return "Sound(" + name + ")";
  143.         } else
  144.             return super.toString();
  145.     }
  146.  
  147.     /** Describes the Sound class' coding information.
  148.       * @see Codable#describeClassInfo
  149.       */
  150.     public void describeClassInfo(ClassInfo info) {
  151.         info.addClass("netscape.application.Sound", 1);
  152.         info.addField(NAME_KEY, STRING_TYPE);
  153.     }
  154.  
  155.     /** Encodes the Sound.  A Sound can only be encoded if it has a name.
  156.       * @see #soundNamed
  157.       * @see Codable#encode
  158.       */
  159.     public void encode(Encoder encoder) throws CodingException {
  160.         if (name == null) {
  161.             throw new CodingException("An encoded Sound must have a name");
  162.         }
  163.  
  164.         encoder.encodeString(NAME_KEY, name);
  165.     }
  166.  
  167.     /** Decodes the Sound.  A Sound can only be decoded if it has a name.
  168.       * @see Codable#decode
  169.       */
  170.     public void decode(Decoder decoder) throws CodingException {
  171.         URL url;
  172.         Application app;
  173.  
  174.         name = decoder.decodeString(NAME_KEY);
  175.         if (name == null) {
  176.             throw new CodingException("A decoded Sound must have a name");
  177.         }
  178.  
  179.         app = Application.application();
  180.         url = app._appResources.urlForSoundNamed(name);
  181.         awtSound = app.applet.getAudioClip(url);
  182.  
  183.         nameSound(name, this);
  184.     }
  185.  
  186.     /** Finishes the Sound's decoding.
  187.       * @see Codable#finishDecoding
  188.       */
  189.     public void finishDecoding() throws CodingException {
  190.     }
  191. }
  192.